package com.chendechao.lib_base

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.viewModelFactory
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

abstract class BaseViewModel<State:Any,Event :Any>(initialState: State):ViewModel() {
    private val _state:MutableStateFlow<State> = MutableStateFlow(initialState)
    //20,更新这个值
    val state:StateFlow<State>  =_state

    fun sendEvent(event: Event){
        viewModelScope.launch {
            //3 协程处理事件
            handleEvent(event)

        }
    }
    // 4 谁实现,谁处理
    protected abstract suspend fun handleEvent(event: Event)

    //19, 状态更新
    protected fun updateState(newState:State){
        _state.value=newState
    }
}